package de.melistik.smugmugsync.model;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SyncFolder {

	File folder;
	boolean syncEnabled = false;
	List<File> imagesInFolder = new ArrayList<File>();
	Date lastSync;
	int filesInFolder;
	Integer categoryId, albumId, filesOnSmugMug;
	String categoryName, albumName;
	List<String> subFolders = new ArrayList<String>();

	public SyncFolder() {

	}

	public SyncFolder(File folder) {
		setFolder(folder);
	}

	public final static String getShortFolderPath(File startSyncPath,
			File deepPath) {
		return deepPath.getAbsolutePath().replace(
				startSyncPath.getAbsolutePath(), "");
	}

	public final static String getFirstFolderInShortPath(File startSyncPath,
			File deppPath) {
		if (SyncFolder.getShortFolderPath(startSyncPath, deppPath).split("\\/").length > 1) {
			return SyncFolder.getShortFolderPath(startSyncPath, deppPath)
					.split("\\/")[1];
		} else {
			return "";
		}
	}

	public final static String getSecondFolderName(File startSyncPath,
			File deepPath) {
		String result = "";
		String[] splitPath = SyncFolder.getShortFolderPath(startSyncPath,
				deepPath).split("\\/");
		if (splitPath.length > 2) {
			result = splitPath[2];
		}
		return result;
	}

	public final static String getThirdToEndFolderNames(File startSyncPath,
			File deepPath) {
		String result = "";
		String[] splitPath = SyncFolder.getShortFolderPath(startSyncPath,
				deepPath).split("\\/");
		if (splitPath.length > 3) {
			for (int x = 2; x < splitPath.length; x++) {
				result += "/" + splitPath[x];
			}
			result.replaceFirst("\\/", "");
		}
		return result;
	}

	public boolean isSyncEnabled() {
		return syncEnabled;
	}

	public void setSyncEnabled(boolean syncEnabled) {
		this.syncEnabled = syncEnabled;
	}

	public File getFolder() {
		return folder;
	}

	public void setFolder(File folder) {
		this.folder = folder;
	}

	public List<File> getImagesInFolder() {
		return imagesInFolder;
	}

	public int getDepthInCompare(File toCompare) {
		int result = -1;
		if (getFolder() != null && toCompare != null) {
			if (getFolder().getAbsolutePath().length() > toCompare
					.getAbsolutePath().length()) {
				String restPath = getFolder().getAbsolutePath().replace(
						toCompare.getAbsolutePath(), "");
				result = restPath.split("\\/").length - 1;
			}
		}
		return result;
	}

	public void setImagesInFolder(List<File> imagesInFolder) {
		this.imagesInFolder = imagesInFolder;
	}

	public void addImageInFolder(File imageInFolder) {
		imagesInFolder.add(imageInFolder);
	}

	public Date getLastSync() {
		return lastSync;
	}

	public void setLastSync(Date lastSync) {
		this.lastSync = lastSync;
	}

	public int getFilesInFolder() {
		return filesInFolder;
	}

	public void setFilesInFolder(int filesInFolder) {
		this.filesInFolder = filesInFolder;
	}

	public Integer getFilesOnSmugMug() {
		return filesOnSmugMug;
	}

	public void setFilesOnSmugMug(Integer filesOnSmugMug) {
		this.filesOnSmugMug = filesOnSmugMug;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public Integer getAlbumId() {
		return albumId;
	}

	public void setAlbumId(Integer albumId) {
		this.albumId = albumId;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public String getAlbumName() {
		return albumName;
	}

	public void setAlbumName(String albumName) {
		this.albumName = albumName;
	}

	public List<String> getSubFolders() {
		return subFolders;
	}

	public void setSubFolders(List<String> subFolders) {
		this.subFolders = subFolders;
	}

	public void initCategoryAlbumNames(File startSyncPath) {
		setCategoryName(getFirstFolderInShortPath(startSyncPath, getFolder()));
		setAlbumName(getSecondFolderName(startSyncPath, getFolder()));
	}

}
